<?php 
defined('RUN_KATANA') or die('Restricted access');
class ProductAttributeValue extends Object
{
	public $AttributeId;
	public $Value;
	public $Postfix;
	public $PostfixPosition;
	public $ValuePrice;
	public $ValueOrder;
	public $Active;
	public $Visible;
	public $TaxId;
	public $TaxValue;
	
	public function ProductAttributeValue($id=0, $attId=0, $val='', $identPostfix='', $identPostfixPosition=0, $valPrice=0, $valOrder=0, $active=0, $visible=0) 
	{ 
		if($id > 0)
		{ self::setId($id); }
		if($attId > 0)
		{ self::setAttributeId($attId); }
		if($val != '')
		{ self::setValue($val); }
		if($identPostfix != '')
		{ self::setPostfix($identPostfix); }
		if($identPostfixPosition > 0)
		{ self::setPostfixPosition($identPostfixPosition); }
		if($valPrice > 0)
		{ self::setValuePrice($valPrice); }
		if($valOrder > 0)
		{ self::setValueOrder($valOrder); }
		if(is_numeric($active))
		{ self::setActive($active); }
		if(is_numeric($visible))
		{ self::setVisible($visible); }
		self::setDataChanged(false);
		self::setTaxId(0);
		self::setTaxValue(1);
	}
	
	/* GET FUNCTIONS */
	public function getAttributeId() { return intval($this->AttributeId); }
	public function getValue() { return $this->Value; }
	public function getPostfix() { return $this->Postfix; }
	public function getPostfixPosition() { return intval($this->PostfixPosition); }
	/*
	public function getValuePrice($tax = false, $round = 0) 
	{ 
		if(($tax == false) || (self::getTaxId() == 0))
		{ 
			if($round > 0)
			{ return round($this->ValuePrice, $round); }
			else
			{ return $this->ValuePrice; }
		} 
		else
		{ return number_format(round(($this->ValuePrice * self::getTaxValue()),1),2,'.',''); }
	}
	*/
	public function getValuePrice($tax = false, $round = 0, $format=true) 
	{ 
		if(is_numeric($this->ValuePrice))
		{
			if(($tax == false) || (self::getTaxId() == 0))
			{ 
				if($round > 0)
				{ return round($this->ValuePrice, $round); }
				else
				{ return $this->ValuePrice; }
			}
			else
			{ 
				if($format)
				{ return number_format(round(($this->ValuePrice * self::getTaxValue()),1),2,'.',''); }
				else
				{ return round(($this->ValuePrice * self::getTaxValue()),1); }
			}
		}
		else
		{ 
			$this->ValuePrice = 0;
			return $this->ValuePrice;
		}
	}
	
	public function getValueOrder() { return intval($this->ValueOrder); }
	public function getActive() { return intval($this->Active); }
	public function getVisible() { return intval($this->Visible); }
	public function getTaxId() { return intval($this->TaxId); }
	public function getTaxValue() { return $this->TaxValue; }
	
	/* SET FUNCTIONS */
	public function setAttributeId($val) { $this->AttributeId=intval($val); }
	public function setValue($val) { $this->Value=$val; }
	public function setPostfix($val) { $this->Postfix=$val; }
	public function setPostfixPosition($val) { $this->PostfixPosition=intval($val); }
	public function setValuePrice($val) { $this->ValuePrice=$val; }
	public function setValueOrder($val) { $this->ValueOrder=intval($val); }
	public function setActive($val) { $this->Active=intval($val); }
	public function setVisible($val) { $this->Visible=intval($val); }
	public function setTaxId($val) { $this->TaxId=$val; }
	public function setTaxValue($val) { $this->TaxValue=$val; }
	
	public function update()
	{
		if((self::getId() > 0) && (self::getDataChanged()))
		{
			$sql  = 'UPDATE '.DB_PERFIX.'product_attribute_values SET ';
			$sql .= '`attribute_id`='.self::getAttributeId().', ';
			if(self::getValue() != '')
			{ $sql .= '`value`="'.self::formatText(self::getValue()).'", '; }
			if(self::getPostfix() != '')
			{ $sql .= '`postfix`="'.self::formatText(self::getPostfix()).'", '; }
			$sql .= '`postfix_position`='.self::getPostfixPosition().', ';
			$sql .= '`value_price`='.self::getValuePrice().', ';
			$sql .= '`value_order`='.self::getValueOrder().', ';
			$sql .= '`active`='.self::getActive().', ';
			$sql .= '`visible`='.self::getVisible().' ';
			$sql .= 'WHERE `id`='.self::getId().';';
			$result = mysql_query($sql);
		}
	}
} 
?>